// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  const nickName=event.nickName;
  const avatarUrl=event.avatarUrl;
  const openId=event.openId;
  const postId=event.postId;
  const text=event.text;
  try {

      await db.collection('comments').add({
        data:{
          nickName: nickName,
          avatarUrl: avatarUrl, 
          openId: openId,
          postId: postId,
          text:text
        }
      });
      return{
        code:0,
        message: '保存成功',
      }
    
    
  } catch (error) {
    console.error('数据库操作失败:', error);
    return {
      code: -1,
      message: '操作失败'
    };
  }

}